package com.googlecode.strutson.utils

import groovy.text.SimpleTemplateEngine
import org.apache.commons.io.IOUtils

public abstract class TemplateUtils {

  //process template files recursively
  static void processAndSaveRecurse(File file, Map bindings) {
    file.eachFileRecurse {
      if (it.isFile() && !it.name.endsWith(".jar")) {
        println "Processing [${it}]"
        processAndSave(it, bindings)
      }
    }
  }

  //reads a template file and process it
  static void processAndSave(File file, Map bindings) {
    def engine = new SimpleTemplateEngine()
    def template = engine.createTemplate(file)
    BufferedWriter bw = new BufferedWriter(new FileWriter(file))
    template.make(bindings).writeTo(bw)
    IOUtils.closeQuietly(bw)
  }
}